<!-- 物理网格 -->
<template>

    <div class="common-layout">
        <el-container>
            <el-header>

                <!-- 面包屑 -->
                <el-breadcrumb separator=">">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>
                        <a href="/">网格管理</a>
                    </el-breadcrumb-item>
                    <el-breadcrumb-item>物理网格</el-breadcrumb-item>
                </el-breadcrumb>
            </el-header>
            <el-container>
                <el-aside width="260px">

                    <el-input v-model="filterText" style="width: 240px" placeholder="过滤关键字" />

                    <el-tree ref="treeRef" style="max-width: 600px" class="filter-tree" :data="data"
                        :props="defaultProps" default-expand-all :filter-node-method="filterNode"
                        @node-click="clickHanlder" />
                </el-aside>
                <el-main>
                    <!--   搜索区域  -->
                    <div style="border: 1px solid black; padding: 40px 5px; margin-bottom: 20px;">
                        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                            <el-form-item label="区域">
                                <el-cascader v-model="searchForm.AreaName" :options="options" :props="props"
                                    @change="handleChange" />
                            </el-form-item>
                            <el-form-item label="网格名称">
                                <el-input v-model="searchForm.GridName" placeholder="输入网格名称" clearable />
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="getTableData()">搜索</el-button>
                            </el-form-item>
                        </el-form>

                    </div>
                    <!-- 表格区域 -->
                    <div style="border: 1px solid black; padding: 5px 5px;">
                        <el-button type="success" @click="dialogAddVisible = true">+新增</el-button>


                        <el-table :data="tableData" style="width: 100%">
                            <el-table-column type="index" label="序号" width="100" />
                            <el-table-column prop="gridNo" label="编号" width="150" />
                            <el-table-column prop="gridName" label="网格名称" width="150" />
                            <el-table-column prop="areaName" label="区域名称" width="150" />
                            <el-table-column fixed="right" label="操作" min-width="120">
                                <template v-slot="scope">
                                    <el-button link type="primary" size="small" @click="del(scope.row.id)">
                                        删除
                                    </el-button>
                                    <el-button link type="primary" size="small"
                                        @click="update(scope.row)">编辑</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>


                </el-main>
            </el-container>
        </el-container>
    </div>


    <!-- 添加物理网格弹窗区域 -->

    <el-dialog v-model="dialogAddVisible" title="新增物理网格" width="500">

        <el-form ref="ruleFormRef" style="max-width: 600px" :model="form" :rules="rules" label-width="auto"
            class="demo-ruleForm" :size="formSize" status-icon>

            <el-form-item label="所在区域" prop='areaName'>
                <el-input v-model="form.areaName" disabled />
            </el-form-item>

            <el-form-item label="网格编号" prop="gridNo">
                <el-input v-model="form.gridNo" />
            </el-form-item>
            <el-form-item label="网格名称" prop="gridName">
                <el-input v-model="form.gridName" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    创建物理网格
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>


    <!-- 编辑物理网格弹窗 -->
    <el-dialog v-model="dialogUpdateVisible" title="修改物理网格" width="500">

        <el-form ref="ruleFormRef" style="max-width: 600px" :model="form" :rules="rules" label-width="auto"
            class="demo-ruleForm" :size="formSize" status-icon>

            <el-form-item label="所在区域" prop='areaName'>
                <el-input v-model="form.areaName" disabled />
            </el-form-item>

            <el-form-item label="网格编号" prop="gridNo">
                <el-input v-model="form.gridNo" />
            </el-form-item>
            <el-form-item label="网格名称" prop="gridName">
                <el-input v-model="form.gridName" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitFormUpdate(ruleFormRef)">
                    更新
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>



</template>


<script lang="ts" setup>
import { onMounted, reactive, ref, watch } from 'vue'
import { ElMessage, ElMessageBox, ElTree } from 'element-plus'
import axios from 'axios';
import http from '@/http/axios';

import type { ComponentSize, FormInstance, FormRules } from 'element-plus'


const form = reactive({
    id: 0,
    "areaId": 0,
    "areaName": '',
    "gridNo": "",
    "gridName": ""
})
const rules = reactive<FormRules>({
    gridName: [
        { required: true, message: '请输入网格名称', trigger: 'blur' },
        { min: 1, max: 20, message: '长度为1-20', trigger: 'blur' },
    ],
    gridNo: [
        { required: true, message: '请输入网格编号', trigger: 'blur' },
        { min: 1, max: 20, message: '长度为1-20', trigger: 'blur' },
    ],
    areaName: [
        { required: true, message: '请选择区域', trigger: 'blur' },
    ]
})

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()


const submitFormUpdate = async (formEl: FormInstance | undefined) => {
    if (!formEl) return//自带校验
    await formEl.validate((valid, fields) => {
        if (valid) {
            //校验通过

            http.put('/T8/PhsicalGrid/UpdatePhsicalGrid', form).then(res => {
                if (res.data.code = 200) {
                    ElMessage.success(res.data.msg);
                    dialogUpdateVisible.value = false;
                    getTableData();
                    form.areaId = 0;
                    form.areaName = '';
                    form.gridName = '';
                    form.gridNo = '';
                } else {
                    ElMessage.error(res.data.msg);
                }
            });

        } else {
            console.log('提交失败!', fields)
        }
    })
}

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return//自带校验
    await formEl.validate((valid, fields) => {
        if (valid) {
            //校验通过

            http.post('/T8/PhsicalGrid/CreatePhsicalGrid', form).then(res => {
                if (res.data.code = 200) {
                    ElMessage.success(res.data.msg);
                    dialogAddVisible.value = false;
                    getTableData();
                } else {
                    ElMessage.error(res.data.msg);
                }
            });

        } else {
            console.log('提交失败!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

//编辑方法
const update = (row: any) => {

    form.areaId = row.areaId
    form.id = row.id
    form.areaName = row.areaName
    form.gridName = row.gridName
    form.gridNo = row.gridNo
    dialogUpdateVisible.value = true;
}

//修改物理网格的弹窗
const dialogUpdateVisible = ref(false)



//删除物理网格

//删除
const del = (id: any) => {


    ElMessageBox.confirm(
        '您确认删除吗?',
        '警告',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {



            http.put('/T8/PhsicalGrid/DelPhsicalGrid', { id: id, "LoginUser": '' })
                .then((res) => {
                    if (res.data.data > 0) {
                        ElMessage.success(res.data.msg);
                        getData();
                        // dialogFormVisible = false;

                    } else {
                        ElMessage.error(res.data.msg);
                    }
                });



        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消操作',
            })
        })


}


//添加物理网格弹窗
const dialogAddVisible = ref(false)

//搜索表单变量
const searchForm = reactive({
    GridName: '',
    AreaName: ''
});

const options = ref([]);
const props = {
    value: 'areaName',
    label: 'areaName',
    children: "children"
}

const handleChange = (node: any) => {
    searchForm.AreaName = node[node.length - 1];
}


const tableData = ref([]);
const getTableData = () => {
    http.get('/T7/PhysicalGrid/GetPhysicalGridPage', { params: searchForm }).then(res => {
        tableData.value = res.data.items
    });

}

//树状
interface Tree {
    [key: string]: any
}

const filterText = ref('')
const treeRef = ref<InstanceType<typeof ElTree>>()

const defaultProps = {
    children: 'children',
    label: 'areaName',
    id: 'id'
}

watch(filterText, (val) => {
    treeRef.value!.filter(val)
})

const filterNode = (value: string, data: Tree) => {
    if (!value) return true
    return data.areaName.includes(value)
}

const data = ref([]);
const getData = () => {

    axios.get('http://localhost:5071/api/Area/GetAreaTree').then(res => {

        data.value = res.data.data
        options.value = res.data.data
    });
}

const clickHanlder = (node, data) => {

    form.areaId = node.id;
    form.areaName = node.areaName
    ElMessageBox.alert("您选择了区域:" + node.areaName);

}

onMounted(() => {
    getData();
    getTableData();
})

</script>